<template>
    <div class="container">
        <div class="app-container">
            <!-- 结构 -->
            <el-tree default-expand-all :data="depts" :props="defaultProps">
                <!-- 节点结构 -->
                <template v-slot="{ data }">
                    <!-- 行 -->
                    <el-row style="width: 100%;height: 100%;" type="flex" justify="space-between" align="middle">
                        <el-col>{{ data.name }}</el-col>
                        <el-col>
                            <span class="tree-manager">{{ data.managerName }}</span>
                            <el-dropdown @command="operatorDept($event,data.id)">
                                <!-- 显示内容区域 -->
                                <span class="el-dropdown-link">
                                    操作 <i class="el-icon-arrow-down" />
                                </span>
                                <el-dropdown-menu slot="dropdown">
                                    <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                                    <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                                    <el-dropdown-item command="del">删除</el-dropdown-item>
                                </el-dropdown-menu>
                            </el-dropdown>
                        </el-col>
                    </el-row>
                </template>
            </el-tree>
        </div>
        <!-- 添加/编辑 部门子组件 -->
        <add-dept 
            ref="addDept"
            :show-dialog.sync="showDialog" 
            @updateDepartment="getDepartment" 
            :current-node-id="currentNodeId">
        </add-dept>
    </div>
</template>

<script>
import {getDepartment}  from '@/api/department'
import { transList2TreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
    name: 'Department',
    components:{AddDept},
    data(){
        return {
            currentNodeId:null,
            depts:[],
            defaultProps:{
                children:"children",
                label:'name'
            },
            showDialog:false
        }
    },
    methods:{
        async getDepartment(){
            const result = await getDepartment()
            // this.depts = result.depts
            this.depts = transList2TreeData(result.depts,0)
        },
        operatorDept(type , id){
            if(type == 'add'){
                this.showDialog = true
            }else if(type == 'edit'){
                 this.showDialog = true
                 this.currentNodeId = Number(id)
                 
                 // 更新props  异步动作
                 // 直接调用了子组件的方法  同步方法
                 // 要在子组件中获取数据
                 // 父组件调用子组件的方法来获取

                 this.$nextTick(()=>{
                    // this  代表 父组件
                    // this.$refs.addDept   代表子组件 的 this
                    
                    this.$refs.addDept.getDepartmentDetail(id)
                 } )
            }
        }
    },
    // 狗子
    created(){
        //获取数据
        this.getDepartment()
    }
}
</script>

<style scoped>
.app-container {
    padding: 30px 140px;
    font-size: 14px;
}

.tree-manager{
    width: 50px;
    display: inline-block;
    margin: 10px;
}
</style>